package com.buobao.spring.jdbc.service;

import com.buobao.spring.jdbc.dao.EmployeeDao;
import com.buobao.spring.jdbc.entity.Employee;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.Date;

public class EmployeeService {
    private EmployeeDao employeeDao;

//    public DataSourceTransactionManager getTransactionManager() {
//        return transactionManager;
//    }
//
//    public void setTransactionManager(DataSourceTransactionManager transactionManager) {
//        this.transactionManager = transactionManager;
//    }
//
//    private DataSourceTransactionManager transactionManager;

    public void insertBatch() {
//        DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
        //开启一个事务，返回事务状态
//        TransactionStatus status = transactionManager.getTransaction(transactionDefinition);
        try {
            for (int i=0;i<10;i++) {
                Employee employee = new Employee();
                employee.setEmpno(9999l+i);
                employee.setEname("new insert"+i);
                employee.setHiredate(new Date());
                employee.setMgr(7698l);
                employee.setSal(8000.0);
                employee.setComm(10.0);
                employee.setJob("test job");
                employee.setEname("张泽涛");
                employee.setDeptno(10l);
                employeeDao.insert(employee);
                if (i==5) {
                    throw new RuntimeException("error");
                }
            }
//            transactionManager.commit(status);
        } catch (Exception e) {
            e.printStackTrace();
//            transactionManager.rollback(status);
        }
    }


    public void insertBatch1(){
        for (int i=0;i<10;i++) {
            Employee employee = new Employee();
            employee.setEmpno(9999l+i);
            employee.setEname("new insert"+i);
            employee.setHiredate(new Date());
            employee.setMgr(7698l);
            employee.setSal(8000.0);
            employee.setComm(10.0);
            employee.setJob("test job");
            employee.setEname("张泽涛");
            employee.setDeptno(10l);
            employeeDao.insert(employee);
            if (i==5) {
                throw new RuntimeException("error");
            }
        }
    }

    public EmployeeDao getEmployeeDao() {
        return employeeDao;
    }

    public void setEmployeeDao(EmployeeDao employeeDao) {
        this.employeeDao = employeeDao;
    }
}
